﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using élénkPróbája.extentions;
using élénkPróbája.Test.dto;
using élénkPróbája.Test.extention;

namespace élénkPróbája.Test
{
    [TestClass]
    public class CascadingFailureTester : IDisposable
    {
        TestValidator validator;
        public CascadingFailureTester()
        {
           ValidatorOptions.CascadeMode = CascadeMode.Continue;
            validator = new TestValidator();
        }

        public void Dispose()
        {
            ValidatorOptions.CascadeMode = CascadeMode.Continue;
        }

        [TestMethod]
        public void Validation_continues_on_failure()
        {
            validator.RuleFor(x => x.Name).NotNull().Equal("Foo");
            var results = validator.Validate(new Company());
            results.Errors.Count.ShouldEqual(2);
        }
        [TestMethod]
        public void Validation_stops_on_first_failure()
        {
            ValidatorOptions.CascadeMode = CascadeMode.StopOnFirstFailure;

            validator.RuleFor(x => x.Name).NotNull().Equal("Foo");
            var results = validator.Validate(new Company());
            results.Errors.Count.ShouldEqual(1);
        }
        [TestMethod]
        public void Validation_continues_on_failure_when_set_to_Stop_globally_and_overriden_at_rule_level()
        {
            ValidatorOptions.CascadeMode = CascadeMode.StopOnFirstFailure;

            validator.RuleFor(x => x.Name).Cascade(CascadeMode.Continue).NotNull().Equal("Foo");
            var results = validator.Validate(new Company());
            results.Errors.Count.ShouldEqual(2);
        }
        [TestMethod]
        public void Validation_stops_on_first_Failure_when_set_to_Continue_globally_and_overriden_at_rule_level()
        {
            ValidatorOptions.CascadeMode = CascadeMode.Continue;
            validator.RuleFor(x => x.Name).Cascade(CascadeMode.StopOnFirstFailure).NotNull().Equal("Foo");
            var results = validator.Validate(new Company());
            results.Errors.Count.ShouldEqual(1);
        }
        [TestMethod]
        public void Validation_continues_to_second_validator_when_first_validator_succeeds_and_cascade_set_to_stop()
        {
            ValidatorOptions.CascadeMode = CascadeMode.StopOnFirstFailure;
            validator.RuleFor(x => x.Name).NotNull().Length(2, 10);
            var result = validator.Validate(new Company() { Name = "x" });
            result.IsValid.ShouldBeFalse();
        }
        [TestMethod]
        public void Validation_stops_on_first_failure_when_set_to_StopOnFirstFailure_at_validator_level()
        {
            validator.CascadeMode = CascadeMode.StopOnFirstFailure;

            validator.RuleFor(x => x.Name).NotNull().Equal("Foo");
            var results = validator.Validate(new Company());
            results.Errors.Count.ShouldEqual(1);
        }
		public void Validation_continues_when_set_to_Continue_at_validator_level() {
			validator.CascadeMode = CascadeMode.Continue;

			validator.RuleFor(x => x.Name).NotNull().Equal("Foo");
			var results = validator.Validate(new Company());
			results.Errors.Count.ShouldEqual(2);
		}
        [TestMethod]
        public void Validation_continues_on_failure_when_set_to_StopOnFirstFailure_at_validator_level_and_overriden_at_rule_level()
        {
            validator.CascadeMode = CascadeMode.StopOnFirstFailure;

            validator.RuleFor(x => x.Name).Cascade(CascadeMode.Continue).NotNull().Equal("Foo");
            var results = validator.Validate(new Company());
            results.Errors.Count.ShouldEqual(2);
        }
        [TestMethod]
        public void Validation_stops_on_failure_when_set_to_Continue_and_overriden_at_rule_level()
        {
            validator.CascadeMode = CascadeMode.Continue;

            validator.RuleFor(x => x.Name).Cascade(CascadeMode.StopOnFirstFailure).NotNull().Equal("Foo");
            var results = validator.Validate(new Company());
            results.Errors.Count.ShouldEqual(1);
        }
        [TestMethod]
        public void Cascade_mode_can_be_set_after_validator_instantiated()
        {
            validator.RuleFor(x => x.Name).NotNull().Equal("Foo");
            validator.CascadeMode = CascadeMode.StopOnFirstFailure;
            var results = validator.Validate(new Company());
            results.Errors.Count.ShouldEqual(1);
        }
    }
}
